Ext.onReady(function(){
	
	var projectInfo = new Ext.form.FormPanel({
		collapsible : false, 
		autoScroll : false, 
		items:[
		{xtype: "textfield", id: "projectId", name: "projectId", hidden: true},	
		{
			xtype : 'displayfield',
			fieldLabel : '房号',
			name:'combinedName'
		},{
			xtype : 'displayfield',
			fieldLabel : '建筑面积',
			name:'buildingArea'
		},{
			xtype : 'displayfield',
			fieldLabel : '分摊面积',
			name:'sharingArea'
		},{
			xtype : 'displayfield',
			fieldLabel : '折前单价',
			id:'price_m'
			
		}]
	});
	
	
	var customerInfo = new Ext.form.FormPanel({
		collapsible : false, 
		autoScroll : false, 
		items:[{
			xtype : 'textfield',
			fieldLabel : '客户姓名',
			id:'customer_name',
			listeners: {   
				render: function(p) {   
				     p.getEl().on('click', function(p){   
				    	 var projectId = Ext.getCmp("projectId").getValue();
				    	 var selectWindow=Ext.create('component.customer.selectWindow',{
		       		 	        callbackFun: function (record) {
		       		 	          var result=Ext.JSON.decode(record);
		       		 	          console.log(result);
		       		 	          Ext.getCmp("customer_name").setValue(result.customerName);
		       		 	          Ext.getCmp("peDocumentNumber").setValue(result.peDocumentNumber);
		       		 	          Ext.getCmp("customerPhone").setValue(result.customerPhone);
		       		 	          Ext.getCmp("peContactAddress").setValue(result.peContactAddress);
		       		 	          Ext.getCmp("customerOriginName").setValue(result.customerOriginName);
		       		 	          $("#custId").val(result.id);
		       		 	        },
	           		 	        projectId: projectId
		       		 	    });
		       		 	    selectWindow.show();
				});   
				}},
				readonly:true
				
		},{
			xtype : 'displayfield',
			fieldLabel : '证件号码',
			id:'peDocumentNumber'
		},{
			xtype : 'displayfield',
			fieldLabel : '联系电话',
			id:'customerPhone'
		},{
			xtype : 'displayfield',
			fieldLabel : '邮寄地址',
			id:'peContactAddress'
		},{
			xtype : 'displayfield',
			fieldLabel : '客户来源',
			id:'customerOriginName'
		}]
	});
	
	
	
	   Ext.create('Ext.Panel', {
	        frame: true,
	        collapsible: false,
	        title: '房产信息',
	        autoScroll:false,
	        renderTo:Ext.getBody(),
	        items:[projectInfo]
	});
	   
	   
	   Ext.create('Ext.Panel', {
	        frame: true,
	        collapsible: false,
	        title: '客户信息',
	        autoScroll:false,
	        renderTo:Ext.getBody(),
	        items:[customerInfo]
	});
	   
	   
	   var fromPanelInfo = Ext.create('Ext.form.FormPanel',{
		   frame: true,
	       collapsible: false,
	       autoScroll:false,
		   items:[{
				xtype : "container",
				layout : "hbox",
				items : [ {
					   xtype:'datefield',
			    	   format:'Y-m-d',
			    	   fieldLabel:'开始有效日期',
			    	   id:'startValidity',
			    	   allowBlank:false,
			    	   blankText:'不能为空'
			    	   
				}, {
					 xtype:'datefield',
			    	 format:'Y-m-d',
			    	 fieldLabel:'结束有效日期',
			    	 id:'endValidity',
			    	 allowBlank:false,
			    	 blankText:'不能为空'
				}]
			},{
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'textarea',
					fieldLabel : '保留原因',
					width:500,
					id:'reason',
					allowBlank:false,
			    	blankText:'不能为空'
				} ]
			},{
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '经办人',
					value:userName
				} ]
			}]
		   
	   });
	   
	   
	   Ext.create('Ext.Panel', {
			collapsible : false, 
			autoScroll : false, 
			defaultType : 'textfield',
			defaults : {
				anchor : '100%'
			},
			fieldDefaults : {
				flex : 1
			},
			title:'保留信息',
	        renderTo:Ext.getBody(),
	        items:[fromPanelInfo],
	        dockedItems: [{
	            xtype: 'toolbar',
	            dock: 'bottom',
	            ui: 'footer',
	            layout: {
	                pack: 'center'
	            },
	            items: [{
	                minWidth: 80,
	                text: '保存',
	                id:'save_button',
	                handler:function(){
	                	var form = fromPanelInfo.getForm();
	                	if($("#custId").val()==null||$("#custId").val()==""){
	                		 Ext.MessageBox.alert("系统提示","请选择客户"); 
	                	}else{
	                		if(form.isValid()){
		                		var parent={
			                			'roomId':roomId,
			                			'customerId':$("#custId").val(),
			                			'starttime':Ext.getCmp("startValidity").getValue(),
			                			'endTime':Ext.getCmp("endValidity").getValue(),
			                			'reason':Ext.getCmp("reason").getValue(),
			                			'retainType':0
			                	};
			                	Ext.Ajax.request({
			                		url:jypath+'/marketing/retain/add',
			                		params:parent,
			                		callback: function (options, success, response) {
			                			 var obj = JSON.parse(response.responseText);
			                			 if(obj.res=="1"){
			    							 Ext.MessageBox.alert("系统提示", "保存成功", function(but) {  
		                					     var tabKey = "ret_"+subscribeId;
		             							 parent.TabControlRemove(tabKey);
		    				                });  
			    						 }else{
			    							 Ext.MessageBox.alert("系统提示",obj.resMsg); 
			    						 }
			                		}
			                	});
		                	}
	                	}
	                
	                	
	                }
	            },{
	                minWidth: 80,
	                text: '返回',
	                handler:function(){
	                	var tabKey = "retein_"+roomId;
	                	parent.TabControlRemove(tabKey);
	                }
	            }]
	        }
	        ]
			
	});
	   
	   
	   projectInfo.getForm().load({
			url:jypath+'/marketing/room/findRoomInfo',
			params:{'roomId':roomId},
			success:function(form, action){
				var obj = action.result.data;
				if(obj.buildingPriceN>0){
					Ext.getCmp("price_m").setValue(obj.buildingPriceN);
				}else if(obj.innerPriceN>0){
					Ext.getCmp("price_m").setValue(obj.innerPriceN);
				}else if(obj.innerPriceN==0&&obj.buildingPriceN==0&&obj.scheme_total_price>0){
					Ext.getCmp("price_m").setValue(obj.scheme_total_price);
				}
			},   
			failure: function(form, action) {
		        Ext.Msg.alert("加载失败", action.result.errorMessage);
		    }

		}); 
});